package com.xc.erp.modules.erp.controller.inoutitem;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xc.erp.common.annotation.Log;
import com.xc.erp.common.utils.SecurityUtils;
import com.xc.erp.modules.erp.domain.AjaxResult;
import com.xc.erp.modules.erp.domain.dto.InOutItemParam;
import com.xc.erp.modules.erp.domain.pojo.JshInOutItem;
import com.xc.erp.modules.erp.service.JshInOutItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 收支项目控制层
 * @author wjw 2022/12/15
 * @DateTime: 2022/12/15 16:56
 */
@Api(tags = "收支项目Api")
@RestController
@RequestMapping("/inOutItem")
public class JshInOutItemController {

    @Autowired
    private JshInOutItemService jshInOutItemService;

    @ApiOperation("查询所有收支订单")
    @GetMapping("list/{current}/{pageSize}")
    public AjaxResult list(InOutItemParam inOutItemParam, @PathVariable Integer current, @PathVariable Integer pageSize){
        IPage<JshInOutItem> page = jshInOutItemService.listPage(inOutItemParam,current,pageSize);
        return AjaxResult.success(page);
    }

    @ApiOperation("检查名称是否已存在")
    @GetMapping("checkIsNameExist/{name}")
    public AjaxResult checkIsNameExist(@PathVariable String name){
        boolean flag = jshInOutItemService.checkIsNameExist(name);
        return AjaxResult.success().put("status",flag);
    }

    @Log(modul = "收支项目",desc = "添加收支记录")
    @ApiOperation("添加收支项目记录")
    @PostMapping("add")
    public AjaxResult add(JshInOutItem jshInOutItem){
        jshInOutItem.setEnabled(true);
        jshInOutItem.setTenantId(SecurityUtils.getLoginUser().getJshUser().getTenantId());
        jshInOutItemService.save(jshInOutItem);
        return AjaxResult.success();
    }

    @Log(modul = "收支项目",desc = "修改收支记录")
    @ApiOperation("修改收支项目记录")
    @PutMapping("update")
    public AjaxResult update(JshInOutItem jshInOutItem){
        jshInOutItemService.updateById(jshInOutItem);
        return AjaxResult.success();
    }

    @Log(modul = "收支项目",desc = "批量修改收支状态")
    @ApiOperation("批量修改状态")
    @PostMapping("batchSetStatus/{status}")
    public AjaxResult batchSetStatus(@PathVariable boolean status,@RequestBody Long[] ids){
        jshInOutItemService.batchSetStatus(status,ids);
        return AjaxResult.success();
    }

    @Log(modul = "收支项目",desc = "批量删除收支记录")
    @ApiOperation("批量删除")
    @DeleteMapping("deleteBatch")
    public AjaxResult removeBatch(@RequestBody Long[] ids){
        jshInOutItemService.removeBatchByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    @Log(modul = "收支项目",desc = "删除收支项目")
    @ApiOperation("删除收支项目记录")
    @DeleteMapping("delete/{id}")
    public AjaxResult remove(@PathVariable Long id){
        jshInOutItemService.removeById(id);
        return AjaxResult.success();
    }
}
